package com.ctshk.sapp.goods.controller;


import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.goods.dto.CommodityDTO;
import com.ctshk.rpc.goods.dto.CommodityOrderDetailsDTO;
import com.ctshk.rpc.goods.dto.CommodityPageDTO;
import com.ctshk.rpc.goods.req.CommodityAddReq;
import com.ctshk.rpc.goods.req.CommodityApprovalProcessReq;
import com.ctshk.rpc.goods.req.CommodityPageReq;
import com.ctshk.rpc.goods.service.ICommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-13
 */
@Api(value="商品管理", tags="商品管理")
@RestController
@RequestMapping("/commodity")
public class CommodityController extends BaseController {

    @DubboReference
    private ICommodityService commodityService;

    @ApiOperation(value = "获取商品成本价", notes = "获取商品成本价")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "commodityId",value = "商品id",required = true,dataType = "Long"),
//            @ApiImplicitParam(name = "currentDateTime",value = "当天时间",required = true,dataType = "DateTimeFormat"),
//            @ApiImplicitParam(name = "specificationsAttributeOneId",value = "属性1id",required = true,dataType = "Long"),
//            @ApiImplicitParam(name = "specificationsAttributeTwoId",value = "属性2id",required = true,dataType = "Long")
//    })
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "获取商品成本价", operDesc = "获取商品成本价")
    @PostMapping("/findCommodityPrice")
    public Result findCommodityPrice(@RequestBody @Valid JSONObject req){
        if (null == req || "{}".equalsIgnoreCase(req.toString())){
            return Result.failed(SystemError.SYS_402);
        }
        Long commodityId = Long.parseLong(req.get("commodityId").toString());
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate currentDateTime = LocalDate.parse(req.get("currentDateTime").toString(),df);
        Long specificationsAttributeOneId = Long.parseLong(req.get("specificationsAttributeOneId").toString());
        Long specificationsAttributeTwoId = Long.parseLong(req.get("specificationsAttributeTwoId").toString());
        return commodityService.findCommodityPriceById(commodityId,currentDateTime,specificationsAttributeOneId,specificationsAttributeTwoId);
    }

    @ApiOperation(value = "新增商品", notes = "新增商品")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增商品", operDesc = "新增商品")
    @PostMapping("/addCommodity")
    public Result addCommodity(@RequestBody @Valid CommodityAddReq obj) throws Exception {
        commodityService.addCommodity(obj, getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "编辑商品", notes = "编辑商品")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "编辑商品", operDesc = "编辑商品")
    @PostMapping("/updateCommodity")
    public Result updateCommodity(@RequestBody @Valid CommodityAddReq obj) throws Exception {
    	commodityService.updateCommodity(obj,getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "更新商品上下架狀態", notes = "更新商品上下架狀態")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "更新商品上下架狀態", operDesc = "更新商品上下架狀態")
    @GetMapping("/updateCommodityStatusById")
    public Result updateCommodityStatusById(@RequestParam(name="id", required=true) Long id, @RequestParam(name="saleStatus", required=true) Integer saleStatus) throws Exception {
        commodityService.updateCommodityStatusById(id, saleStatus,getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "商品审批-状态更新", notes = "商品审批-状态更新")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "商品审批-状态更新", operDesc = "商品审批-状态更新")
    @PostMapping("/updateCommodityApprovalStauts")
    public Result updateCommodityApprovalStauts(@RequestBody CommodityApprovalProcessReq obj) throws Exception {
        commodityService.commodityApprovalProcess(obj,getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "根据ID查询商品", notes = "根据ID查询商品")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "根据ID查询商品", operDesc = "根据ID查询商品")
    @GetMapping("/findCommodityById/{id}")
    public Result<CommodityDTO> findCommodityById(@PathVariable(name = "id") Long id) throws Exception {
        return commodityService.findCommodityById(id);
    }

    @ApiOperation(value = "门店订单功能-商品详情页查询", notes = "门店订单功能-商品详情页查询")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "门店订单功能-商品详情页查询", operDesc = "门店订单功能-商品详情页查询")
    @GetMapping("/findCommodityOrderDetailsById/{id}")
    public Result<CommodityOrderDetailsDTO> findCommodityOrderDetailsById(@PathVariable(name = "id") Long id) throws Exception {
        return commodityService.findCommodityOrderDetailsById(id,getCurrentUser(getRequest()).getId());
    }

    @ApiOperation(value = "分页查询商品列表", notes = "分页查询商品列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "分页查询商品列表", operDesc = "分页查询商品列表")
    @PostMapping("/findPageList")
    public Result<PageResponse<CommodityPageDTO>> findPageList(@RequestBody CommodityPageReq pageReq) {
        PageResponse<CommodityPageDTO> response = commodityService.findCommodityPageList(pageReq,getCurrentUser(getRequest()));
        return Result.success(response);
    }

    @ApiOperation(value = "根据商品ID查询规格和属性列表", notes = "根据商品ID查询规格和属性列表")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "根据商品ID查询规格和属性列表", operDesc = "根据商品ID查询规格和属性列表")
    @GetMapping("/findCommodityAttributesListById/{id}")
    public Result<JSONObject> findCommodityAttributesListById(@PathVariable("id") Long id){
        return Result.success(commodityService.findCommodityAttributesListById(id));
    }
}
